/**
 * Created by whw on 2017/1/10.
 */
$(function () {
    var getPalMsg = '/myhome/shop/getMyShop',
        upData = '/myhome/shop/uploadLogoAndProfile',
        main = $('.contanier'),
        upLogo = $('.upLogo'),
        notShop = $('.notShop'),
        homePage = $('#homePage'),
        oldMsg = '',
        imgList = [],
        url = '',
        requestData = function (obj,fun,fn) {
            $.ajax({
                url: host + obj.getUrl,
                type: 'post',
                data: obj.data,
                success: function (data) {
                    fun(data)
                },
                error:function (data) {
                    fn && fn(data)
                }
            });
        },
        uuidLocal = Cookie.getCookie()['uuid']                 // 获取用户信息;
        ;
    if(!uuidLocal){                                         // 没有登陆直接跳转
        layer.open({
            content: '您还没有登录，请先登录！'
            ,btn: ['去登录', '去首页']
            ,shadeClose:false
            ,yes: function(index){
                window.location.href = 'login.html';
                layer.close(index);
            }
            ,no: function (index) {
                window.location.href = 'index.html';
                layer.close(index);
            }
        });
        return;
    }
    ImporInfor();   // 获取用户个人信息
    requestData({
        getUrl :getPalMsg,
        data : Infornation + '&fromWhere=' + 2
    }, function (data) {
        if(data.data){
            var data = data.data
            $('.logo').find('img').eq(0).attr('src', data.shopLogo || "img/user.png")
            homePage.attr('href', data.homepage)
            oldMsg = data.enterpriseIdentity.companyProfile;
            $('#companyProfile').val(data.enterpriseIdentity.companyProfile)
            if(data.authenticationStatus !==1 && data.auditStatus !== 1 && data.auditStatus != 3){
                $('.enterprise').removeClass('hidden')
            }
            main.removeClass('hidden')
        }else {
            notShop.removeClass('hidden')
        }
    });
    // 上传 logo
    $('.contanier li').eq(0).on('click', function () {
        $(this).parents('.contanier').addClass('hidden')
        $('.upLogo').removeClass('hidden')
    })
    $('input[type="file"]').on('change', function () {
        var _this = this,
            file = Array.prototype.slice.call(this.files)[0],
            index = $(this).index('input[type="file"]'),
            fileName = this.name;
        if (!this.files.length){
            $('.uploadImg').eq(index).show()
            return;
        }else{
            $('.uploadImg').eq(index).hide()
        }
        lrz(file,{
            quality: 0.5,
            fieldName: 'imageFile'
        })
            .then(function (rst) {
                // 处理成功会执行
                console.log(rst)
                $('.upImage').css('background-image','url('+rst.base64+')')
                url = rst.base64;
                $('.upImage').css('background-size','cover')
                imgList[0] = rst.formData;
            })
            .catch(function (err) {
                // 处理失败会执行
                console.log(err)
            })
            .always(function () {
                // 不管是成功失败，都会执行
            });
    })
    $('.contanier button').on('click', function () {
       var companyProfile = $('#companyProfile').val().trim() || '',
           shopLogo = $('.logo img').data('img') || '',
           parmes = '';
       parmes = 'companyProfile='+companyProfile+'&shopLogo='+shopLogo+'&'+Infornation;
       if((companyProfile != oldMsg) || shopLogo){
           $.ajax({
               url:host + upData,
               data:parmes,
               type:'post',
               beforeSend:function () {
                   layer.open({
                       type: 2
                       ,content: '资料更新上传中...'
                       ,shadeClose:false
                   });
               },
               success: function (data) {
                   if(data.success){
                       layer.closeAll();
                       lay.msg('更新成功')
                       setTimeout(function () {
                           window.location.href = 'personal.html';
                       },500)
                   }
               },
               error: function () {
                   // lay.msg('上传资料失败；请刷新后重试；')
                   // setTimeout(function () {
                   //     window.location.reload();
                   // },500)
                   return;
               }
           })
       }else {
           window.location.href = 'personal.html';
       }
    })
    $('.upLogo button').on('click', function () {
        $.ajax({
            url:hostImgUpload + '?isAddWatermark=false',
            type:"post",
            data:imgList[0],
            processData:false,// 不要处理发送的数据
            contentType:false,// 不要设置请求头
            dataType:'json',
            beforeSend:function () {
                layer.open({
                    type: 2
                    ,content: '图片上传中...'
                    ,shadeClose:false
                });
            },
            success:function(data){
                layer.closeAll();
                console.log(data)
                upLogo.addClass('hidden')
                $('.logo img').eq(0).attr('src',url).data('img',data.message)
                main.removeClass('hidden')
            },
            error:function(e){
                lay.msg('上传资料失败；请刷新后重试；')
                setTimeout(function () {
                    window.location.reload();
                },500)
                return;
                // window.clearInterval(timer);
            }
        });
    })

})
